package com.example.project1;

import java.util.*;
import java.io.*;

public class Main {
  private static Scanner scanner = new Scanner(System.in);
  private static StudentManager manager = new StudentManager();

  public static void main(String[] args) {
    try {
      manager.loadFromFile();
    } catch (Exception e) {
      System.out.println("加载数据失败: " + e.getMessage());
    }

    while (true) {
      showMenu();
      int choice = getIntInput("请选择操作: ");

      try {
        switch (choice) {
          case 1:
            addStudent();
            break;
          case 2:
            deleteStudent();
            break;
          case 3:
            updateStudent();
            break;
          case 4:
            queryStudent();
            break;
          case 5:
            showAllStudents();
            break;
          case 6:
            showSortedStudents();
            break;
          case 7:
            saveData();
            break;
          case 0:
            System.out.println("感谢使用学生管理系统，再见！");
            return;
          default:
            System.out.println("无效的选择，请重新输入");
        }
      } catch (Exception e) {
        System.out.println("操作失败: " + e.getMessage());
      }

      System.out.println("\n按回车键继续...");
      scanner.nextLine();
    }
  }

  private static void showMenu() {
    System.out.println("\n========== 学生管理系统 ==========");
    System.out.println("1. 添加学生");
    System.out.println("2. 删除学生");
    System.out.println("3. 修改学生");
    System.out.println("4. 查询学生");
    System.out.println("5. 显示所有学生");
    System.out.println("6. 按成绩排序");
    System.out.println("7. 保存数据");
    System.out.println("0. 退出系统");
  }

  private static void addStudent() throws Exception {
    System.out.println("\n----- 添加学生 -----");
    String id = getStringInput("学号: ");
    String name = getStringInput("姓名: ");
    int age = getIntInput("年龄: ");
    String gender = getStringInput("性别: ");
    double score = getDoubleInput("成绩: ");

    Student student = new Student(id, name, age, gender, score);
    manager.addStudent(student);
  }

  private static void deleteStudent() throws Exception {
    System.out.println("\n----- 删除学生 -----");
    String id = getStringInput("请输入要删除的学生学号: ");
    manager.deleteStudent(id);
  }

  private static void updateStudent() throws Exception {
    System.out.println("\n----- 修改学生信息 -----");
    String oldId = getStringInput("请输入要修改的学生学号: ");

    System.out.println("请输入新的学生信息:");
    String id = getStringInput("学号: ");
    String name = getStringInput("姓名: ");
    int age = getIntInput("年龄: ");
    String gender = getStringInput("性别: ");
    double score = getDoubleInput("成绩: ");

    Student newStudent = new Student(id, name, age, gender, score);
    manager.updateStudent(oldId, newStudent);
  }

  private static void queryStudent() {
    System.out.println("\n----- 查询学生 -----");
    System.out.println("1. 按学号查询");
    System.out.println("2. 按姓名查询");
    int choice = getIntInput("请选择查询方式: ");

    if (choice == 1) {
      String id = getStringInput("请输入学号: ");
      Student student = manager.findStudentById(id);
      if (student != null) {
        System.out.println("查询结果: " + student);
      } else {
        System.out.println("未找到该学号的学生");
      }
    } else if (choice == 2) {
      String name = getStringInput("请输入姓名: ");
      List<Student> students = manager.findStudentsByName(name);
      if (students.isEmpty()) {
        System.out.println("未找到匹配的学生");
      } else {
        System.out.println("查询结果 (" + students.size() + " 条):");
        for (Student student : students) {
          System.out.println(student);
        }
      }
    } else {
      System.out.println("无效的选择");
    }
  }

  private static void showAllStudents() {
    System.out.println("\n----- 所有学生信息 -----");
    List<Student> students = manager.getAllStudents();
    if (students.isEmpty()) {
      System.out.println("暂无学生信息");
    } else {
      System.out.println("共 " + students.size() + " 条记录:");
      for (Student student : students) {
        System.out.println(student);
      }
    }
  }

  private static void showSortedStudents() {
    System.out.println("\n----- 按成绩排序的学生信息 -----");
    List<Student> students = manager.sortByScore();
    if (students.isEmpty()) {
      System.out.println("暂无学生信息");
    } else {
      System.out.println("按成绩从高到低排序:");
      for (int i = 0; i < students.size(); i++) {
        System.out.println((i + 1) + ". " + students.get(i));
      }
    }
  }

  private static void saveData() {
    try {
      manager.saveToFile();
    } catch (IOException e) {
      System.out.println("保存数据失败: " + e.getMessage());
    }
  }

  // 辅助方法
  private static String getStringInput(String prompt) {
    System.out.print(prompt);
    return scanner.nextLine().trim();
  }

  private static int getIntInput(String prompt) {
    while (true) {
      try {
        System.out.print(prompt);
        return Integer.parseInt(scanner.nextLine());
      } catch (NumberFormatException e) {
        System.out.println("请输入有效的整数");
      }
    }
  }

  private static double getDoubleInput(String prompt) {
    while (true) {
      try {
        System.out.print(prompt);
        return Double.parseDouble(scanner.nextLine());
      } catch (NumberFormatException e) {
        System.out.println("请输入有效的数字");
      }
    }
  }
}
